﻿Imports CoreCode
Imports CoreCode.Connection

Public Class TRAN_AR_SI

    Private clsDBCon As New Connection
    Private sSQLAdapter(1) As System.Data.SqlClient.SqlDataAdapter
    Private sDataSet As New DataSet

    Private WithEvents sBindSource As New BindingSource
    Private bsChild As New BindingSource

    Private lBinded As Boolean
    Private drv As DataRowView

    Private dtgName As String
    Private nBookMark As Single

    Public eAct As eAction

    Private Sub TRAN_AR_SI_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        sSQLAdapter(0) = New System.Data.SqlClient.SqlDataAdapter
        sSQLAdapter(1) = New System.Data.SqlClient.SqlDataAdapter
        nBookMark = 0
    End Sub

    Public Function TBConnect() As Boolean
        With clsDBCon
            .DatabaseName = .TranDatabaseName
            .DatabasePassword = .ConnectionPassword
            .DatabaseServer = .ConnectionServer
            .DatabaseServerType = ServerType.MSSQL
            .DatabaseUserName = .ConnectionUserName
            .Connect(.SQLCn)
            .DatabaseName = .SetDatabaseName
            .Connect(.SQLCnSet)
            .DatabaseName = .SecDatabaseName
            .Connect(.SQLCnSec)
            .DatabaseName = .TranDatabaseName
            Return .Connect(.SQLCnTran)
        End With
    End Function
    Public Function TBNew(ByVal obj As Object) As Boolean
        obj.tag = "newedit"
        eAct = eAction.NewRec
        TBNew = False
        Try
            With clsDBCon
                TBConnect()

                sDataSet.Tables.Clear()
                .DataUnbind(Me)
                .ObjectControl(Me, ObjectMethod.Cleardata)
                .ObjectControl(Me, ObjectMethod.ObjectLock, False)


                .OpenRecord(.SQLCnTran, sSQLAdapter(1), sDataSet, "*", tran_ar_si_t.Name)

                .SetIncrementSeed(sDataSet, tran_ar_si_t, "nID")

                .DatagridPopup(tran_ar_si_t, .SQLCnTran)
                bsChild.DataSource = sDataSet.Tables(tran_ar_si_t.Name)
                .DataGridBind(tran_ar_si_t, bsChild)

                GetLocalChild()

                TBNew = True

            End With
        Catch ex As Exception
            MsgBox(ex.Message)
        End Try
    End Function
    Public Function TBEdit(ByVal obj As Object) As Boolean
        obj.tag = "newedit"
        eAct = eAction.EditRec
        TBEdit = False
        With clsDBCon
            .ObjectControl(Me, ObjectMethod.ObjectLock, False)
            TBEdit = True

        End With
    End Function
    Public Function TBDelete(ByVal obj As Object) As Boolean
        With clsDBCon
            TBDelete = False
            If MsgBox("Are you sure you want to delete this item?", MsgBoxStyle.Question + MsgBoxStyle.YesNo, TitleHeader) = MsgBoxResult.Yes Then
                .ExecuteNonQuery(.SQLCnTran, "UPDATE " & Me.Name & " SET lDelete=1 WHERE " & nID.Name & "='" & nID.Text & "'")

                obj.tag = "clear"
                TBFind(obj)
                obj.tag = "find"
                TBFind(obj)

                TBDelete = True

            End If
        End With
    End Function
    Public Function TBSave(ByVal obj As Object) As Boolean
        Try
            TBSave = False
            With clsDBCon
                .lKillRecursive = False
                If .MandatoryOK(Me) = False Then Exit Function
                .ExecuteStoredProc(.SQLCnTran, "sp" & Me.Name, Me, eAct)
                Dim s As String = .GetSearchString(Me, True)
                nID.Text = .GetValueFrTable("nID", Me.Name, s)
                If eAct = eAction.EditRec Then
                    For Each row As DataRow In sDataSet.Tables(tran_ar_si_t.Name).Select("nRefID=" & nID.Text, "nID")
                        row("nRefID") = nID.Text
                    Next
                Else
                    For Each row As DataRow In sDataSet.Tables(tran_ar_si_t.Name).Select("nRefID=0", "nID")
                        row("nRefID") = nID.Text
                    Next
                End If

                bsChild.EndEdit()
                sSQLAdapter(1).Update(sDataSet.Tables(tran_ar_si_t.Name))
                .ObjectControl(Me, ObjectMethod.ObjectLock, True)
                GetLocalChild()
                MsgBox(SaveMsg, MsgBoxStyle.Information, TitleHeader)

                TBSave = True

            End With
        Catch ex As Exception
            MsgBox(ex.Message)
        End Try

    End Function
    Public Function TBUndo(ByVal obj As Object) As Boolean
        TBUndo = False
        With clsDBCon
            obj.tag = "clear"
            TBFind(obj)

            TBUndo = True

        End With
    End Function
    Public Function TBFind(ByVal obj As Object) As Boolean
        Try
            TBFind = False
            With (clsDBCon)
                TBConnect()
                If obj.tag = "clear" Or obj.tag = "" Then
                    .WaitWindow(WaitLoadingMsg, True)
                    obj.tag = "find"
                    sDataSet.Tables.Clear()
                    .ObjectControl(Me, ObjectMethod.ObjectLock, True)

                    Dim sSearch As String
                    sSearch = .GetSearchString(Me)
                    .OpenRecord(.SQLCnTran, sSQLAdapter(0), sDataSet, "*", Me.Name, "lDelete=0 " & IIf(sSearch = "", "", " and " & sSearch))
                    .OpenRecord(.SQLCnTran, sSQLAdapter(1), sDataSet, "*", tran_ar_si_t.Name)

                    .SetIncrementSeed(sDataSet, tran_ar_si_t, "nID")

                    .DatagridPopup(dtgList, .SQLCnTran, Me.Name)
                    .DatagridPopup(tran_ar_si_t, .SQLCnTran)

                    sBindSource.DataSource = sDataSet.Tables(Me.Name)
                    .Databind(Me, sBindSource)
                    bsChild.DataSource = sDataSet.Tables(tran_ar_si_t.Name)
                    .DataGridBind(tran_ar_si_t, bsChild)

                    GetLocalChild()
                    sBindSource.Position = nBookMark
                    .WaitWindow("", False)
                Else
                    obj.tag = "clear"
                    .ObjectControl(Me, ObjectMethod.ObjectLock, False)
                    .DataUnbind(Me)
                    sDataSet.Tables.Clear()
                    .ObjectControl(Me, ObjectMethod.Cleardata)

                End If
                TBFind = True
            End With
        Catch ex As Exception
            MsgBox(ex.Message)
        End Try
    End Function
    Public Sub TBNewItem()
        Try
            If dtgName = tran_ar_si_t.Name Then
                With tran_ar_si_t
                    .EditMode = DataGridViewEditMode.EditOnF2
                    Dim row As DataRow = sDataSet.Tables(.Name).NewRow
                    row("nRefID") = IIf(nID.Text = "", 0, nID.Text)
                    sDataSet.Tables(.Name).Rows.Add(row)
                End With
                GetLocalChild()
            End If

        Catch ex As Exception
            MsgBox(ex.Message)
        End Try
    End Sub
    Public Sub TBRemoveItem()
        Try
            If dtgName = tran_ar_si_t.Name Then
                bsChild.RemoveCurrent()
                GetLocalChild()
            End If
        Catch ex As Exception
            MsgBox(ex.Message)
        End Try
    End Sub
    Public Sub GetLocalChild()
        With clsDBCon
            .GetChild("nRefID=" & IIf(nID.Text = "", 0, nID.Text), bsChild)
        End With
        sDataSet.Tables(tran_ar_si_t.Name).DefaultView.RowFilter = "nRefID=" & IIf(nID.Text = "", 0, nID.Text)
    End Sub

    Private Sub bNavi_ItemClicked(ByVal sender As Object, ByVal e As System.Windows.Forms.ToolStripItemClickedEventArgs) Handles bNavi.ItemClicked
        Dim lRet As Boolean
        Select Case e.ClickedItem.Name
            Case "tsNew"
                lRet = TBNew(mode)
            Case "tsEdit"
                lRet = TBEdit(mode)
            Case "tsDelete"
                lRet = TBDelete(mode)
            Case "tsSave"
                lRet = TBSave(mode)
            Case "tsUndo"
                lRet = TBUndo(mode)
            Case "tsFind"
                lRet = TBFind(mode)
        End Select
        If lRet = True Then TBControl(e.ClickedItem)
    End Sub

    Public Sub TBControl(ByVal obj As Object)
        Select Case obj.name
            Case "tsNew", "tsEdit", "tsDelete", "tsSave", "tsUndo", "tsFind"
                tsNew.Enabled = False
                tsEdit.Enabled = False
                tsDelete.Enabled = False
                tsSave.Enabled = False
                tsUndo.Enabled = False
                tsFind.Enabled = False
                tsMoveFirst.Enabled = False
                tsMoveLast.Enabled = False
                tsMoveNext.Enabled = False
                tsMovePrev.Enabled = False
                tsCurrentPos.Enabled = False
        End Select

        Select Case obj.Name
            Case "tsNew"
                tsSave.Enabled = True
                tsUndo.Enabled = True
                mode.Tag = "newedit"
            Case "tsEdit"
                tsSave.Enabled = True
                tsUndo.Enabled = True
                mode.Tag = "newedit"
            Case "tsDelete"
                If mode.Tag = "find" Then
                    tsNew.Enabled = True
                    tsEdit.Enabled = True
                    tsDelete.Enabled = True
                    tsFind.Enabled = True
                    tsCurrentPos.Enabled = True
                    tsMoveFirst.Enabled = True
                    tsMoveLast.Enabled = True
                    tsMoveNext.Enabled = True
                    tsMovePrev.Enabled = True
                    mode.Tag = "find"
                Else
                    tsNew.Enabled = True
                    tsFind.Enabled = True
                    mode.Tag = "clear"
                End If
            Case "tsSave"
                tsNew.Enabled = True
                tsEdit.Enabled = True
                tsDelete.Enabled = True
                tsFind.Enabled = True
                tsCurrentPos.Enabled = True
                tsMoveFirst.Enabled = True
                tsMoveLast.Enabled = True
                tsMoveNext.Enabled = True
                tsMovePrev.Enabled = True
                mode.Tag = "find"
            Case "tsUndo"
                If mode.Tag = "new" Then
                    tsNew.Enabled = True
                    tsFind.Enabled = True
                    mode.Tag = "clear"
                Else
                    tsNew.Enabled = True
                    tsEdit.Enabled = True
                    tsDelete.Enabled = True
                    tsFind.Enabled = True
                    tsCurrentPos.Enabled = True
                    tsMoveFirst.Enabled = True
                    tsMoveLast.Enabled = True
                    tsMoveNext.Enabled = True
                    tsMovePrev.Enabled = True
                    mode.Tag = "find"
                End If
            Case "tsFind"
                If mode.Tag = "find" Then
                    tsNew.Enabled = True
                    tsEdit.Enabled = True
                    tsDelete.Enabled = True
                    tsFind.Enabled = True
                    tsCurrentPos.Enabled = True
                    tsMoveFirst.Enabled = True
                    tsMoveLast.Enabled = True
                    tsMoveNext.Enabled = True
                    tsMovePrev.Enabled = True
                    mode.Tag = "find"
                Else
                    tsNew.Enabled = True
                    tsFind.Enabled = True
                    mode.Tag = "clear"
                End If
        End Select
    End Sub

    Private Sub sBindSource_PositionChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles sBindSource.PositionChanged
        If mode.Tag = "find" Then GetLocalChild()
        nBookMark = sBindSource.Position
    End Sub

    Private Sub tran_ar_si_t_MouseDown(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles tran_ar_si_t.MouseDown
        With tran_ar_si_t
            If .EditMode = DataGridViewEditMode.EditProgrammatically Then Exit Sub
            dtgName = .Name
            If e.Button = MouseButtons.Right Then
                mnuStrip.Show(tran_ar_si_t, New Drawing.Point(e.X, e.Y))
            End If
        End With
    End Sub

    Private Sub mnuStrip_ItemClicked(ByVal sender As Object, ByVal e As System.Windows.Forms.ToolStripItemClickedEventArgs) Handles mnuStrip.ItemClicked
        Select Case e.ClickedItem.Text
            Case "New Item"
                TBNewItem()
            Case "Remove Item"
                TBRemoveItem()
        End Select
    End Sub

  

    Private Sub TabControlPanel11_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TabControlPanel11.Click

    End Sub

    Private Sub btnCustPick_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCustPick.Click
        TBConnect()
        With clsDBCon
            Dim sResult As String = .PopupPicker(2, .SQLCnSet)
            If Trim$(sResult) = "" Then Exit Sub
            Dim s() As String = Split(Mid$(sResult, 2, Len(sResult) - 2), "▪")
            cCustID.Text = s(0)
            cCustName.Text = s(1)
            s = Nothing
        End With
    End Sub


    Private Sub btnCustAddressPick_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCustAddressPick.Click
        If Trim$(cCustID.Text) = "" Then Exit Sub
        TBConnect()
        With clsDBCon
            Dim sResult As String = .PopupPicker(5, .SQLCnSet, " ID='" & Trim$(cCustID.Text) & "'")
            If Trim$(sResult) = "" Then Exit Sub
            Dim s() As String = Split(Mid$(sResult, 2, Len(sResult) - 2), "▪")
            cCustAddress.Text = s(1)
            s = Nothing
        End With
    End Sub

    Private Sub btnMSPick_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnMSPick.Click
        TBConnect()
        With clsDBCon
            Dim sResult As String = .PopupPicker(3, .SQLCnSet)
            If Trim$(sResult) = "" Then Exit Sub
            Dim s() As String = Split(Mid$(sResult, 2, Len(sResult) - 2), "▪")
            cMSID.Text = s(0)
            s = Nothing
        End With
    End Sub

    Private Sub btnSMPick_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSMPick.Click
        TBConnect()
        With clsDBCon
            Dim sResult As String = .PopupPicker(4, .SQLCnSet)
            If Trim$(sResult) = "" Then Exit Sub
            Dim s() As String = Split(Mid$(sResult, 2, Len(sResult) - 2), "▪")
            cSMID.Text = s(0)
            cSMName.Text = s(1)
            s = Nothing
        End With
    End Sub

    Private Sub btnPayTermPick_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnPayTermPick.Click
        TBConnect()
        With clsDBCon
            Dim sResult As String = .PopupPicker(7, .SQLCnSet, , True)
            If Trim$(sResult) = "" Then Exit Sub
            Dim s() As String = Split(Mid$(sResult, 2, Len(sResult) - 2), "▪")
            cPayTerms.Text = s(0)
            .SQLCn = .SQLCnSet
            cPayTerms1.Text = .GetValueFrTable("cValue", "set_payment_terms", "nID='" & Trim$(cPayTerms.Text) & "'")
            .SQLCn = .SQLCnTran
            s = Nothing

        End With
    End Sub

    Private Sub btnPayTypePick_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnPayTypePick.Click
        TBConnect()
        With clsDBCon
            Dim sResult As String = .PopupPicker(8, .SQLCnSet, , True)
            If Trim$(sResult) = "" Then Exit Sub
            Dim s() As String = Split(Mid$(sResult, 2, Len(sResult) - 2), "▪")
            cPaymentType.Text = s(0)
            .SQLCn = .SQLCnSet
            cPaymentType1.Text = .GetValueFrTable("cValue", "set_payment_types", "nID='" & Trim$(cPaymentType.Text) & "'")
            .SQLCn = .SQLCnTran
            s = Nothing
        End With
    End Sub

    Private Sub btnCurTypePick_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCurTypePick.Click
        TBConnect()
        With clsDBCon
            Dim sResult As String = .PopupPicker(6, .SQLCnSet, , True)
            If Trim$(sResult) = "" Then Exit Sub
            Dim s() As String = Split(Mid$(sResult, 2, Len(sResult) - 2), "▪")
            cCurType1.Text = s(1)
            cCurType.Text = s(0)
            s = Nothing

        End With
    End Sub
End Class
